local test = UnitTest:New("08DongleError")
local Dongle = DongleStub("Dongle-1.0")

local L = {
	["BAD_ARGUMENT"] = "bad argument #%d to '%s' (%s expected, got %s)",
}

-- This function is copied from Dongle.lua, and must be kept up to date
local function argcheck(value, num, ...)
	if type(num) ~= "number" then
		error(L["BAD_ARGUMENT"]:format(2, "argcheck", "number", type(num)), 1)
	end

	for i=1,select("#", ...) do
		if type(value) == select(i, ...) then return end
	end

	local types = strjoin(", ", ...)
	local name = string.match(debugstack(2,2,0), ": in function [`<](.-)['>]")
	error(L["BAD_ARGUMENT"]:format(num, name, types, type(value)), 3)
end

local function assert(level,condition,message)
	if not condition then
		error(message,level)
	end
end

local Library = {}

function Library:API_Function(arg1)
	argcheck(arg1, 2, "number")
end

function Library:API_Fail(arg1)
	assert(3, reg, "This call to 'API_Fail' is expected to fail.")
end

local function argcheck_help()
	Library:API_Function()
end

local function assert_help()
	Library:API_Fail()
end

function test:dongle_argcheck_error()
	assert_fails(argcheck_help, "08DongleError.lua:40: bad argument #2 to 'API_Function'")
end

function test:dongle_assert_error()
	assert_fails(assert_help, "08DongleError.lua:44", "This call to 'API_Fail' is expected to fail.")
end

test:Run()